import 'package:flutter_module/common/base/page/BasicPage.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import '../../../common/base/page/BasePage.dart';
import '../../../res/colors.dart';
import '../../../res/image.dart';
import 'add_book_logic.dart';

class AddBookPage extends BasePage {
  final logic = Get.put(AddBookLogic());

  String bookName = "";

  AddBookPage({Key? key, required this.bookName}) : super(key: key) {
    logic.setParameter(bookName);
  }

  @override
  State<AddBookPage> createState() => _AddBookPageState();
}

class _AddBookPageState extends BasicPage<AddBookPage, AddBookLogic> {
  @override
  AddBookLogic get currentLogic => widget.logic;

  @override
  String screenName() {
    return "添加书籍页";
  }

  @override
  Widget body(BuildContext context) {
    return _addBookBody(context);
  }

  @override
  List<Widget>? actions() {
    return _addBookAction(context);
  }

  Widget _addBookBody(BuildContext context) {
    return SingleChildScrollView(
      keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
      child: Container(
        color: Colors.white,
        child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              GestureDetector(
                behavior: HitTestBehavior.opaque,
                onPanDown: (_) {
                  FocusScope.of(context).requestFocus(FocusNode());
                },
                child: _addBookPotato(),
              ),
              _divider(),
              _addBookTitle(),
              _divider(),
              _addBookAuthor(),
              _divider(),
              _addBookPress(),
              _divider(),
              _addBookPressTime(context),
              _divider(),
              _addBookIsbn(),
              _divider(),
            ]),
      ),
    );
  }

  Row _addBookPotato() {
    return Row(
      mainAxisSize: MainAxisSize.max,
      children: [
        Container(
          height: 120,
          color: Colors.white,
          alignment: Alignment.centerLeft,
          padding: const EdgeInsets.only(left: 20),
          child: const Text.rich(
            TextSpan(
                text: "书籍封面",
                style: TextStyle(
                  fontSize: 15,
                  color: YouthColors.color_453938,
                ),
                children: [
                  TextSpan(
                    text: " *",
                    style: TextStyle(
                      fontSize: 15,
                      color: YouthColors.color_F65C15,
                    ),
                  )
                ]),
          ),
        ),
        const Spacer(),
        GestureDetector(
          onTap: () => currentLogic.takePotato(),
          child: Container(
            width: 70,
            height: 101,
            margin: const EdgeInsets.only(right: 23),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(4),
                color: YouthColors.color_EEEEEE),
            child: Obx(
              () => (currentLogic.localImageByte.value?.isNotEmpty == true)
                  ? ClipRRect(
                      borderRadius: const BorderRadius.all(Radius.circular(4)),
                      child: Image.memory(
                        currentLogic.localImageByte.value ?? Uint8List(0),
                        fit: BoxFit.cover,
                      ),
                    )
                  : Container(
                      alignment: Alignment.center,
                      child: SvgPicture.asset(
                        '${YouthImages.imageDir}ic_book_add.svg',
                      ),
                    ),
            ),
          ),
        )
      ],
    );
  }

  List<Widget> _addBookAction(BuildContext context) {
    List<Widget> actions = [];
    Widget submitBtn = MaterialButton(
      onPressed: () {
        currentLogic.commit(context);
      },
      child: Container(
        alignment: Alignment.centerRight,
        margin: const EdgeInsets.only(right: 15),
        child: const Text(
          "提交",
          textAlign: TextAlign.center,
          style: TextStyle(
              fontSize: 17,
              color: YouthColors.color_F65C15,
              fontWeight: FontWeight.bold),
        ),
      ),
    );
    actions.add(submitBtn);
    return actions;
  }

  Divider _divider() {
    return const Divider(
      height: 0.5,
      // Divider 组件高度
      thickness: 0.5,
      // 分割线线宽，分割线居于 Divider 中心
      indent: 16,
      // 分割线左侧间距
      endIndent: 16,
      // 分割线右侧间距
      color: YouthColors.color_EDEBEB, // 分割线颜色
    );
  }

  Row _addBookTitle() {
    return Row(
      mainAxisSize: MainAxisSize.max,
      children: [
        Container(
          height: 54,
          color: Colors.white,
          alignment: Alignment.centerLeft,
          padding: const EdgeInsets.only(left: 20),
          child: const Text.rich(
            TextSpan(
                text: "书名",
                style: TextStyle(
                  fontSize: 15,
                  color: YouthColors.color_453938,
                ),
                children: [
                  TextSpan(
                    text: " *",
                    style: TextStyle(
                      fontSize: 15,
                      color: YouthColors.color_F65C15,
                    ),
                  )
                ]),
          ),
        ),
        const SizedBox(
          width: 53,
        ),
        Expanded(
          child: TextField(
            controller: currentLogic.bookNameController,
            maxLength: 30,
            textAlign: TextAlign.end,
            textInputAction: TextInputAction.newline,
            keyboardType: TextInputType.text,
            inputFormatters: [
              FilteringTextInputFormatter.deny(currentLogic.regexp)
            ],
            decoration: const InputDecoration(
              counterText: '',
              hintText: '请输入',
              hintStyle: TextStyle(
                fontSize: 15,
                color: YouthColors.color_A29B9B,
              ),
              border: InputBorder.none,
            ),
          ),
        ),
        const SizedBox(
          width: 22,
        ),
      ],
    );
  }

  Row _addBookAuthor() {
    return Row(
      mainAxisSize: MainAxisSize.max,
      children: [
        Container(
          height: 54,
          color: Colors.white,
          alignment: Alignment.centerLeft,
          padding: const EdgeInsets.only(left: 20),
          child: const Text.rich(
            TextSpan(
                text: "作者",
                style: TextStyle(
                  fontSize: 15,
                  color: YouthColors.color_453938,
                ),
                children: [
                  TextSpan(
                    text: " *",
                    style: TextStyle(
                      fontSize: 15,
                      color: YouthColors.color_F65C15,
                    ),
                  )
                ]),
          ),
        ),
        const SizedBox(
          width: 53,
        ),
        Expanded(
          child: TextField(
            controller: currentLogic.bookAuthorController,
            textAlign: TextAlign.end,
            maxLength: 30,
            textInputAction: TextInputAction.newline,
            keyboardType: TextInputType.text,
            inputFormatters: [
              FilteringTextInputFormatter.deny(currentLogic.regexp)
            ],
            decoration: const InputDecoration(
              counterText: '',
              hintText: '请输入',
              hintStyle: TextStyle(
                fontSize: 15,
                color: YouthColors.color_A29B9B,
              ),
              border: InputBorder.none,
            ),
          ),
        ),
        const SizedBox(
          width: 22,
        ),
      ],
    );
  }

  Row _addBookPress() {
    return Row(
      mainAxisSize: MainAxisSize.max,
      children: [
        Container(
          height: 54,
          color: Colors.white,
          alignment: Alignment.centerLeft,
          padding: const EdgeInsets.only(left: 20),
          child: const Text(
            "出版社",
            style: TextStyle(
              fontSize: 15,
              color: YouthColors.color_453938,
            ),
          ),
        ),
        const SizedBox(
          width: 53,
        ),
        Expanded(
          child: TextField(
            controller: currentLogic.bookPressController,
            maxLength: 30,
            textAlign: TextAlign.end,
            textInputAction: TextInputAction.newline,
            keyboardType: TextInputType.text,
            inputFormatters: [
              FilteringTextInputFormatter.deny(currentLogic.regexp)
            ],
            decoration: const InputDecoration(
              counterText: '',
              hintText: '请输入',
              hintStyle: TextStyle(
                fontSize: 15,
                color: YouthColors.color_A29B9B,
              ),
              border: InputBorder.none,
            ),
          ),
        ),
        const SizedBox(
          width: 22,
        ),
      ],
    );
  }

  Row _addBookPressTime(BuildContext context) {
    return Row(
      mainAxisSize: MainAxisSize.max,
      children: [
        Container(
          height: 54,
          color: Colors.white,
          alignment: Alignment.centerLeft,
          padding: const EdgeInsets.only(left: 20),
          child: const Text(
            "出版时间",
            style: TextStyle(
              fontSize: 15,
              color: YouthColors.color_453938,
            ),
          ),
        ),
        Expanded(
          child: Container(),
        ),
        GestureDetector(
          onTap: () => currentLogic.showDateSelector(context),
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              SizedBox(
                width: 78,
                child: TextField(
                  enabled: false,
                  maxLength: 30,
                  controller: currentLogic.bookPressDateController,
                  inputFormatters: [
                    FilteringTextInputFormatter.deny(currentLogic.regexp)
                  ],
                  decoration: const InputDecoration(
                    counterText: '',
                    hintText: '请选择',
                    hintStyle: TextStyle(
                      fontSize: 15,
                      color: YouthColors.color_A29B9B,
                    ),
                    border: InputBorder.none,
                  ),
                  onTap: null,
                  style: const TextStyle(
                    fontSize: 15,
                    color: YouthColors.color_17191D,
                  ),
                  textAlign: TextAlign.right,
                ),
              ),
              Image.asset(
                YouthImages.icRightArrow,
                width: 32,
                height: 32,
              ),
              const SizedBox(
                width: 10,
              )
            ],
          ),
        ),
      ],
    );
  }

  Row _addBookIsbn() {
    return Row(
      mainAxisSize: MainAxisSize.max,
      children: [
        Container(
          height: 54,
          color: Colors.white,
          alignment: Alignment.centerLeft,
          padding: const EdgeInsets.only(left: 20),
          child: const Text(
            "ISBN",
            style: TextStyle(
              fontSize: 15,
              color: YouthColors.color_453938,
            ),
          ),
        ),
        const SizedBox(
          width: 53,
        ),
        Expanded(
          child: TextField(
            maxLength: 30,
            controller: currentLogic.bookIsbnController,
            textAlign: TextAlign.end,
            textInputAction: TextInputAction.newline,
            keyboardType: TextInputType.text,
            inputFormatters: [
              FilteringTextInputFormatter.deny(currentLogic.regexp)
            ],
            decoration: InputDecoration(
              counterText: '',
              hintText: '请输入',
              hintStyle: TextStyle(
                fontSize: 15,
                color: YouthColors.color_A29B9B,
              ),
              border: InputBorder.none,
            ),
          ),
        ),
        const SizedBox(
          width: 22,
        ),
      ],
    );
  }
}
